<script>
    $(document).ready(function() {
    
        $( "ul.droptrue" ).sortable({
            connectWith: "ul",
            items: "li:not(.disabled)"
        });
 
        $( "ul.dropfalse" ).sortable({
            connectWith: "ul",
            dropOnEmpty: false
        });
 
        $( ".sortable-event" ).disableSelection();
        
        $('#results').submit(function() {
            $('#events').val($('#found-events').html());
        });
    });
</script>

<style>
    .sortable-event { list-style-type: none; margin: 5px; padding: 0; float: left; margin-right: 10px; background: #eee; padding: 5px; min-height: 100px; }
    .sortable-event li { margin: 5px; padding: 5px; font-size: 0.9em; }
    .ui-state-highlight {cursor: pointer; }
    .ui-state-default {font-weight: bold; }
</style>

<div id="found-events">
    <?php
    $events_per_day = ceil($num_events / ($days + 1));
    $events_added_total = 0;
    for ($day = 0; $day <= $days; $day++):
        ?>
        <ul class="droptrue sortable-event" id="day<?php echo $day + 1; ?>">
            <?php $datestring = ($day + 1) . '. päev - ' . date('d.m.Y', strtotime($start_date) + $day * 86400); ?>
            <li class="ui-state-default disabled"><?php echo $datestring; ?></li> 
            <?php
            for ($i = $events_added_total, $events_added = 0; $events_added < $events_per_day && $i < $num_events; $i++) :
                $event = $events[$i];
                $events_added_total++;
                $events_added++;
                ?>
                <li class="ui-state-highlight"><?php echo $event->getRaw(); ?></li>
            <?php endfor; ?>
        </ul>
    <?php endfor; ?>

    <br style="clear: both;" />
</div>

<?php
echo form_open('files/add_results', array('id' => 'results'));
echo form_input(array('name' => 'events', 'type' => 'hidden', 'id' => 'events'));
echo form_hidden('full_path', $full_path);
echo form_hidden('competition_id', $competition_id);
echo form_submit('add_results', 'Lisa tulemused');
echo form_close();
?>
<?php foreach ($events as /* @var $event Event */ $event) : ?>
    <table class="klubid">
        <tr><th colspan="7"><?php echo $event->toStringEst(); ?></th></tr>
        <tr>
            <th>Koht</th>
            <th>Nimi</th>
            <th>Sünnia.</th>
            <th>Klubi</th>
            <th>Tulemus</th>
            <th>FINA</th>
            <th>Result ms</th>
            <th>Ath_id</th>
        </tr>
        <?php
        $rank = 1;
        foreach ($results as /* @var $result Result */ $result) {
            if ($result->getEventRaw() == $event->getRaw()) {
                if ($result->getAthleteID() == 0 && $result->getRelay() == null) {
                    echo '<tr class="athlete_missing" >';
                } else {
                    echo '<tr>';
                }
                echo '<td>' . ($result->getResultMS() == MAX_INT ? '---' : $rank) . '</td>';
                if ($result->getAthleteID() != 0) {
                    $athlete = Athlete::fromID($result->getAthleteID());
                    echo '<td>' . $athlete->getLastName() . ', ' . $athlete->getFirstName() . '</td>';
                    echo '<td>' . $athlete->getBirthYear() . '</td>';
                    echo '<td>' . $result->getTeam() . '</td>';
                } else if ($result->getRelay() == NULL) {
                    echo '<td>' . $result->getAthleteName() . '</td>';
                    echo '<td>' . $result->getAthleteAge() . '</td>';
                    echo '<td>' . $result->getTeam() . '</td>';
                } else {
                    echo '<td>' . $result->getTeam() . '</td>';
                    echo '<td colspan="2">' . $result->getRelay()->getTeam() . '</td>';
                }
                echo '<td>' . $result->getResult() . '</td>';
                echo '<td>' . $result->getFina() . '</td>';
                echo '<td>' . $result->getResultMS() . '</td>';
                if ($result->getAthleteID() != 0) {
                    echo '<td>' . $result->getAthleteID() . '</td>';
                    echo '</td></tr>';
                    print_r($result->getRelay());
                } else if ($result->getRelay() != null) {
                    $r = $result->getRelay();
                    $i = 1;
                    foreach (array($r->getAthleteOneID(), $r->getAthleteTwoID(), $r->getAthleteThreeID(), $r->getAthleteFourID()) as $id) {
                        $athlete = Athlete::fromID($id);
                        if ($athlete != null) {
                            echo '<tr><td></td><td> ' . $i . ') ' . $athlete->getLastName() . ', ' . $athlete->getFirstName() . '</td>';
                            echo '<td colspan="5">' . $athlete->getBirthYear() . '</td>';
                            echo '<td>' . $id . '</td></tr>';
                        } else {
                            echo '<tr class="athlete_missing" ><td></td><td>' . $i . ') <td></tr>';
                        }
                        $i++;
                    }
                }
                $rank++;
            }
        }
        ?>
    </table>
<?php endforeach; ?>